package org.abratuhi.kvngroupware.gui;

import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.RegistrationController;
import org.abratuhi.kvngroupware.model.Team;
import org.abratuhi.kvngroupware.spring.SpringUtilities;
import org.jdom.Element;

@SuppressWarnings("serial")
public class GUIRegistration extends JPanel{
	
	KVNGroupwareClient kvnclient;
	RegistrationController controller;
	private JFrame frame;
	
	JLabel labelUserId = new JLabel();
	JLabel labelUserPassword = new JLabel();
	JLabel labelUserName = new JLabel();
	JLabel labelUserSurname = new JLabel();
	JLabel labelUserTeam = new JLabel();
	
	JTextField fieldUserId = new JTextField();
	JTextField fieldUserPassword = new JTextField();
	JTextField fieldUserName = new JTextField();
	JTextField fieldUserSurname = new JTextField();
	JComboBox comboUserTeam = new JComboBox();
	
	JLabel labelTeamName = new JLabel();
	JLabel labelTeamTown = new JLabel();
	JLabel labelTeamCountry = new JLabel();
	
	JTextField fieldTeamName = new JTextField();
	JTextField fieldTeamTown = new JTextField();
	JTextField fieldTeamCountry = new JTextField();
	
	public JButton buttonCreateUser = new JButton();
	public JButton buttonCreateTeam = new JButton();
	
	
	public GUIRegistration(KVNGroupwareClient kvnclient, Properties locale){
		super();
		
		this.kvnclient = kvnclient;
		this.controller = new RegistrationController(this.kvnclient, this);
		
		this.buttonCreateTeam.setText(locale.getProperty("registration.button.team.create"));
		this.buttonCreateUser.setText(locale.getProperty("registration.button.user.create"));
		this.labelTeamCountry.setText(locale.getProperty("registration.label.team.country"));
		this.labelTeamName.setText(locale.getProperty("registration.label.team.name"));
		this.labelTeamTown.setText(locale.getProperty("registration.label.team.town"));
		this.labelUserId.setText(locale.getProperty("registration.label.user.id"));
		this.labelUserPassword.setText(locale.getProperty("registration.label.user.password"));
		this.labelUserName.setText(locale.getProperty("registration.label.user.name"));
		this.labelUserSurname.setText(locale.getProperty("registration.label.user.surname"));
		this.labelUserTeam.setText(locale.getProperty("registration.label.user.team"));
		
		this.buttonCreateTeam.addActionListener(controller);
		this.buttonCreateUser.addActionListener(controller);
		
		JPanel panelCreateUser = new JPanel();
		panelCreateUser.setBorder(new TitledBorder(locale.getProperty("registration.border.newuser")));
		panelCreateUser.setLayout(new SpringLayout());
		panelCreateUser.add(labelUserId);
		panelCreateUser.add(fieldUserId);
		panelCreateUser.add(labelUserPassword);
		panelCreateUser.add(fieldUserPassword);
		panelCreateUser.add(labelUserName);
		panelCreateUser.add(fieldUserName);
		panelCreateUser.add(labelUserSurname);
		panelCreateUser.add(fieldUserSurname);
		panelCreateUser.add(labelUserTeam);
		panelCreateUser.add(comboUserTeam);
		panelCreateUser.add(new JLabel());
		panelCreateUser.add(buttonCreateUser);
		SpringUtilities.makeCompactGrid(panelCreateUser, 6, 2, 0, 0, 0, 0);
		
		JPanel panelCreateTeam = new JPanel();
		panelCreateTeam.setBorder(new TitledBorder(locale.getProperty("registration.border.newteam")));
		panelCreateTeam.setLayout(new SpringLayout());
		panelCreateTeam.add(labelTeamName);
		panelCreateTeam.add(fieldTeamName);
		panelCreateTeam.add(labelTeamTown);
		panelCreateTeam.add(fieldTeamTown);
		panelCreateTeam.add(labelTeamCountry);
		panelCreateTeam.add(fieldTeamCountry);
		panelCreateTeam.add(new JLabel());
		panelCreateTeam.add(buttonCreateTeam);
		SpringUtilities.makeCompactGrid(panelCreateTeam, 4, 2, 0, 0, 0, 0);
		
		this.setLayout(new SpringLayout());
		this.add(panelCreateUser);
		this.add(panelCreateTeam);
		SpringUtilities.makeCompactGrid(this, 1, 2, 0, 0, 0, 0);
		
		this.setVisible(true);
		
	}
	
	public String[] getNewUserData(){
		return new String[]{fieldUserId.getText(), fieldUserPassword.getText(), fieldUserName.getText(), fieldUserSurname.getText(), comboUserTeam.getSelectedItem().toString()};
	}
	
	public String[] getNewTeamData(){
		return new String[]{fieldTeamName.getText(), fieldTeamTown.getText(), fieldTeamCountry.getText()};
	}
	
	public void addTeamEntry(Element eTeam){
		comboUserTeam.removeActionListener(controller);
		comboUserTeam.addItem(Team.fromElement(eTeam).getId());
		comboUserTeam.setSelectedIndex(-1);
		comboUserTeam.addActionListener(controller);
	}
	
	public void updateTeamEntries(Element[] eTeams){
		comboUserTeam.removeActionListener(controller);
		comboUserTeam.removeAllItems();
		for(int i=0; i<eTeams.length; i++) comboUserTeam.addItem(Team.fromElement(eTeams[i]).getId());
		comboUserTeam.setSelectedIndex(-1);
		comboUserTeam.addActionListener(controller);
	}
	
	//TODO
	/*public void paint(Graphics g){
		super.paint(g);
	}*/
	
	public void createJFrame(){
		frame = new JFrame("..:: KVNGroupware Client ::.."+"\t"+"Registration");
		frame.getContentPane().add(this);
		frame.setVisible(true);
		frame.addWindowListener(this.controller);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}
	

}
